package com.scy.java.thread.eatbuns;
//顾客线程类，主要负责吃包子
public class CustomerClass extends Thread{
    private BaoZi baoZi;

    public CustomerClass(BaoZi baoZi) {
        this.baoZi = baoZi;
    }
    //统计顾客的人数
    int count = 0;
    //设置线程任务吃包子
    @Override
    public void run() {
        while (true){
            synchronized (baoZi){
                //对包子状态进行判断
                if (!baoZi.flag){
                    try {
                        //没包子就等待
                        baoZi.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                //顾客被唤醒之后，开始吃包子
                System.out.println(
                        "顾客"+count+"正在吃："+baoZi.skin
                                +baoZi.stuffing+"包子");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                //吃完包子后，修改包子的状态
                baoZi.flag = false ;
                System.out.println("顾客"+count+"已经将"+baoZi.skin
                        +baoZi.stuffing+"包子，吃完了。");
                count++;
                //唤醒包子铺生产包子
                baoZi.notify();
                System.out.println("包子铺开始生产包子");
            }
        }
    }
}
